﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SaludOcupacional.Model;
using SaludOcupacional.Exceptions;
using System.Collections.ObjectModel;
using System.Data;

namespace SaludOcupacional.DataAccess
{
    public class CronogramasDataAccess : IDisposable
    {
        private DatabaseManager dbManager;

        public void Dispose()
        {
            dbManager.Dispose();
        }
        public CronogramasDataAccess()
        {
            dbManager = new DatabaseManager();
        }

        public void CrearCita(Cronograma cronogramas)
        {
            if (cronogramas == null)
            {
                throw new LoginException("Invalid Input Parameters");
            }

            string sqlSentence = "INSERT INTO [dbo].[Cronogramas]([ci_empleado],[fecha],[analisis])VALUES ( ";
            sqlSentence += "@Ci, @Fecha, @Analisis  )";

            dbManager.Command.CommandText = sqlSentence;
            dbManager.Command.CommandType = System.Data.CommandType.Text;


            dbManager.Command.Parameters.AddWithValue("@Ci", cronogramas.CiEmpleado);
            dbManager.Command.Parameters.AddWithValue("@Fecha", cronogramas.Fecha);
            dbManager.Command.Parameters.AddWithValue("@Analisis", cronogramas.Analisis);

            try
            {
                int value = dbManager.NonQuery();

                if (value < 1)
                {
                    throw new LoginException("The record wasn't registered");
                }
            }
            catch (Exception e)
            {
                throw new LoginException("Error creating a user" + e.Message, e);
            }
        }

        public Collection<Cronograma> BuscarCitasPorCi(int ci)
        {


            string sqlsentence = "SELECT  * FROM [Cronogramas] WHERE ci_empleado = @Ci";

            dbManager.Command.CommandText = sqlsentence;
            dbManager.Command.Parameters.AddWithValue("@Ci", ci);

            Collection<Cronograma> lista = new Collection<Cronograma>();

            try
            {
                DataSet dataSet = dbManager.Query();

                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    DataRowCollection reader = dataSet.Tables[0].Rows;

                    for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                    {

                        lista.Add(new Cronograma
                        {
                            CodCronograma = Convert.ToInt32(reader[i]["cod_cronograma"], System.Globalization.CultureInfo.InvariantCulture),
                            CiEmpleado = Convert.ToInt32(reader[i]["ci_empleado"], System.Globalization.CultureInfo.InvariantCulture),
                            Fecha = reader[i]["fecha"].ToString(),
                            Analisis = reader[i]["analisis"].ToString()
                            


                        });
                    }
                }
            }


            catch (Exception)
            {
                throw;
            }

            return lista;
        }

        public void Editarcronograma(Cronograma cronograma)
        {
            if (cronograma == null)
            {
                throw new LoginException("The empleado can´t be null");
            }

            string sqlSentence =
                "UPDATE [Cronogramas]  SET [fecha] = @Fecha WHERE [ci_empleado] = @Ci and [analisis] = @Analisis";

            dbManager.Command.CommandText = sqlSentence;
            dbManager.Command.CommandType = System.Data.CommandType.Text;


            dbManager.Command.Parameters.AddWithValue("@Fecha", cronograma.Fecha);
            dbManager.Command.Parameters.AddWithValue("@Ci", cronograma.CiEmpleado);
            dbManager.Command.Parameters.AddWithValue("@Analisis", cronograma.Analisis);
  


            try
            {
                int value = dbManager.NonQuery();
                if (value < 1)
                {
                    throw new LoginException("The change wasn't registered");
                }

            }
            catch (Exception e)
            {
                throw new LoginException("Error edit profile", e);
            }
        }

    }
}
